package com.x_memteor.base_master.utils.power;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import java.util.Arrays;

public class PowerOnOffManager {
    public static final String INTENT_ACTION_CLEARONTIME = "android.intent.ClearOnTime";
    public static final String INTENT_ACTION_POWERONOFF = "android.intent.action.setpoweronoff";
    public static final String INTENT_ACTION_POWERONTIME = "android.intent.PowerOnTime";
    public static final String PERSIST_SYS_POWERONOFF = "persist.sys.poweronoff";
    public static final String PERSIST_SYS_POWERONOFFBAK = "persist.sys.poweronoffbak";
    public static final String SHUTDOWN_ACTION = "android.intent.action.shutdown";
    private static final String TAG = "PowerOnOffManager";
    private static PowerOnOffManager powerOnOffManager;
    private Context mContext;

    private PowerOnOffManager(Context context) {
        this.mContext = context;
    }

    public static synchronized PowerOnOffManager getInstance(Context context) {
        PowerOnOffManager powerOnOffManager = null;
        synchronized (PowerOnOffManager.class) {
            if (powerOnOffManager == null) {
                powerOnOffManager = new PowerOnOffManager(context);
            }
            powerOnOffManager = powerOnOffManager;
        }
        return powerOnOffManager;
    }

    @SuppressLint("WrongConstant")
    public void setPowerOnTime(int year, int month, int day, int hourOfDay, int minute) {
        if (Utils.validate(year, month, day, hourOfDay, minute)) {
            Intent intent = new Intent(INTENT_ACTION_POWERONTIME);
            intent.putExtra("year", year);
            intent.putExtra("month", month);
            intent.putExtra("day", day);
            intent.putExtra("hour", hourOfDay);
            intent.putExtra("minute", minute);
            this.mContext.sendBroadcast(intent);
            Log.d(TAG, "设置开机时间成功");
            return;
        }
        Toast.makeText(this.mContext, "设置的开机时间有误", 1).show();
    }

    public void shutdown() {
        Intent intent = new Intent();
        intent.setAction(SHUTDOWN_ACTION);
        this.mContext.sendBroadcast(intent);
    }

    public void clearPowerOnTime() {
        this.mContext.sendBroadcast(new Intent(INTENT_ACTION_CLEARONTIME));
    }

    public boolean isSetPowerOnTime() {
        return !"off:on:".equals(getPowerOnTime());
    }

    public String getPowerOnTime() {
        return Utils.getValueFromProp(PERSIST_SYS_POWERONOFF);
    }

    public String getLastestPowerOnTime() {
        return Utils.getValueFromProp(PERSIST_SYS_POWERONOFFBAK);
    }

    public void setPowerOnOff(int[] powerOnTime, int[] powerOffTime) {
        Intent intent = new Intent(INTENT_ACTION_POWERONOFF);
        intent.putExtra("timeon", powerOnTime);
        intent.putExtra("timeoff", powerOffTime);
        intent.putExtra("enable", true);
        this.mContext.sendBroadcast(intent);
        Log.d(TAG, "poweron:" + Arrays.toString(powerOnTime) + "/ poweroff:" + Arrays.toString(powerOffTime));
    }

    public void cancelPowerOnOff(int[] powerOnTime, int[] powerOffTime) {
        Intent intent = new Intent(INTENT_ACTION_POWERONOFF);
        intent.putExtra("timeon", powerOnTime);
        intent.putExtra("timeoff", powerOffTime);
        intent.putExtra("enable", false);
        this.mContext.sendBroadcast(intent);
        Log.d(TAG, "poweron:" + Arrays.toString(powerOnTime) + "/ poweroff:" + Arrays.toString(powerOffTime));
    }
}